home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15074 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.sunquest.com!kitk!kitk
  2. From: kitk@mudshark.sunquest.com (Kit Kauffmann)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Bletcherous kludge!
  5. Date: Wed, 3 Apr 1996 08:17:54
  6. Organization: Sunquest
  7. Message-ID: <kitk.2158.00084CAA@mudshark.sunquest.com>
  8. References: <4jhr2b$9ou@portal.gmu.edu>
  9. NNTP-Posting-Host: kkauffma.sunquest.com
  10. X-Newsreader: Trumpet for Windows [Version 1.0 Rev A]
  11.  
  12. In article <4jhr2b$9ou@portal.gmu.edu> rraffer1@osf1.gmu.edu (Ryan M Rafferty) writes:
  13.  
  14. >I am trying to get the contents of a custom String class to be converted
  15. >into numeric data, ie float.  The String class consists of :
  16.  
  17. >Buffer:  a null-terminated array of char
  18.  
  19. >and several methods for common string stuff, including overloaded << and >>
  20. >operators.  The code listed below is the only method I've been able to come up
  21. >with that works, but it is really an awful hack that outputs the contents of
  22. >the String instance to a file and then reads it back in to do the conversion.
  23.  
  24. >I tried creating an instance of iostream, inputting to it from the String
  25. >instance, and then outputting from it into a float variable, but it just
  26. >didn't work.  Any suggestions on how to do this the right way would be
  27. >greatly appreciated!
  28.  
  29. >Ryan Rafferty 
  30.  
  31. >#include "String.h"
  32.  
  33. >float String::Number() {
  34.  
  35. >  float i = 0;
  36.  
  37. >  // this conversion method is, in its current state, a HORRIBLE and
  38. >  // AWFUL kludge to convert the contents of a String instance to a
  39. >  // numeric type.  It doesn't do any error checking, but what's worse,
  40. >  // it USES A TEMPORARY FILE !!!! I'm so mad at myself!  But I can't
  41. >  // seem to be able to bend the will of the iostreams class to do what
  42. >  // I want!
  43.  
  44. >  ofstream fout("./__temp");
  45. >  fout << Buffer;
  46. >  fout.close();
  47. >  ifstream fin("./__temp");
  48. >  fin >> i;
  49. >  fin.close();
  50.  
  51. >  //      YECH!  A feeble attempt to hide my shame...
  52. >  //      execl("/bin/rm", "rm", "./__temp", (char *) 0);
  53.  
  54. >  return i;
  55. >};
  56.  
  57. double String::Number()
  58. {
  59.    return atof( Buffer );
  60. }
  61.  
  62.  
  63. Kit Kauffmann - kitk@mudshark.sunquest.com
  64. AKA 73363,447 (Compu$erve)
  65.  
  66. Finger me for my public key
  67.